package com.ruoyi.wb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wb.domain.WbMaintainCompany;
import com.ruoyi.wb.domain.WbMaintainCompanyUser;
import com.ruoyi.wb.dto.WbMaintainCompanyDto;
import com.ruoyi.wb.dto.WbMaintainCompanyUserAddDto;
import com.ruoyi.wb.dto.WbRoleDto;
import com.ruoyi.wb.enmus.RoleNameEnums;
import com.ruoyi.wb.mapper.WbMaintainCompanyMapper;
import com.ruoyi.wb.mapper.WbMaintainCompanyUserMapper;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.service.IWbMaintainCompanyUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 维保公司Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-14
 */
@Service
@AllArgsConstructor
public class WbMaintainCompanyServiceImpl implements IWbMaintainCompanyService 
{
    private final WbMaintainCompanyMapper wbMaintainCompanyMapper;

    private final WbMaintainCompanyUserMapper wbMaintainCompanyUserMapper;

    private final IWbMaintainCompanyUserService iWbMaintainCompanyUserService;



    /**
     * 查询维保公司
     * 
     * @param id 维保公司主键
     * @return 维保公司
     */
    @Override
    public WbMaintainCompany selectWbMaintainCompanyById(Long id)
    {
        return wbMaintainCompanyMapper.selectWbMaintainCompanyById(id);
    }

    /**
     * 查询维保公司列表
     * 
     * @param wbMaintainCompany 维保公司
     * @return 维保公司
     */
    @Override
    public List<WbMaintainCompany> selectWbMaintainCompanyList(WbMaintainCompany wbMaintainCompany)
    {
        //查询当前用户有没有和维保公司绑定如果有则只能查询自己的维保公司
        LoginUser loginUser = SecurityUtils.getLoginUser();
        WbMaintainCompanyUser wbMaintainCompanyUser = new WbMaintainCompanyUser();
        wbMaintainCompanyUser.setUserId(loginUser.getUserId());

        List<WbMaintainCompanyUser> wbMaintainCompanyUsers = wbMaintainCompanyUserMapper.selectWbMaintainCompanyUserList(wbMaintainCompanyUser);

         if (!CollectionUtils.isEmpty(wbMaintainCompanyUsers)){
             wbMaintainCompany.setId(wbMaintainCompanyUsers.get(0).getMaintainCompanyId());
         }
        startPage();
        return wbMaintainCompanyMapper.selectWbMaintainCompanyList(wbMaintainCompany);
    }

    /**
     * 新增维保公司
     */
    @Override
    @Transactional
    public Long insertWbMaintainCompany(WbMaintainCompanyDto wbMaintainCompanyDto)
    {
        if (ObjectUtils.isEmpty(wbMaintainCompanyDto)){
            return 1L;
        }
         if (ObjectUtils.isEmpty(wbMaintainCompanyDto.getPhoneNumber())){
            throw new RuntimeException("联系方式必填");
        }
        WbMaintainCompany wbMaintainCompany = BeanUtil.copyProperties(wbMaintainCompanyDto, WbMaintainCompany.class);

        wbMaintainCompany.setCreateTime(DateUtils.getNowDate());
        int i = wbMaintainCompanyMapper.insertWbMaintainCompany(wbMaintainCompany);
        if (i==0){
            throw new RuntimeException("新增维保公司失败");
        }


        WbMaintainCompanyUserAddDto wbMaintainCompanyUserAddDto = new WbMaintainCompanyUserAddDto();
        wbMaintainCompanyUserAddDto.setMaintainCompanyId(wbMaintainCompany.getId());
        wbMaintainCompanyUserAddDto.setUserName(StringUtils.isEmpty(wbMaintainCompanyDto.getUserName())?wbMaintainCompanyDto.getPhoneNumber().toString():wbMaintainCompanyDto.getUserName());
        wbMaintainCompanyUserAddDto.setNickName(wbMaintainCompany.getContact());
        wbMaintainCompanyUserAddDto.setPhonenumber(wbMaintainCompany.getPhoneNumber().toString());
        wbMaintainCompanyUserAddDto.setPassword(StringUtils.isEmpty(wbMaintainCompanyDto.getPassWord())?null:wbMaintainCompanyDto.getPassWord());
        //wbMaintainCompanyUserAddDto.setUserId(wbMaintainCompanyDto.getUserId());
        List<WbRoleDto> wbRoles= new ArrayList<>();
        WbRoleDto wbRoleDto = new WbRoleDto();
        wbRoleDto.setRoleId(Long.parseLong(RoleNameEnums.MAINTAIN_ADMIN.getType().toString()));
        wbRoleDto.setRoleName(RoleNameEnums.MAINTAIN_ADMIN.getDescribe());
        wbRoles.add(wbRoleDto);
        wbMaintainCompanyUserAddDto.setWbRoles(wbRoles);
        int j = iWbMaintainCompanyUserService.insertWbMaintainCompanyUser(wbMaintainCompanyUserAddDto);
        if (j==0){
            throw new RuntimeException("新增角色失败");
        }
        return wbMaintainCompany.getId();
    }

    /**
     * 修改维保公司
     * 
     * @param wbMaintainCompany 维保公司
     * @return 结果
     */
    @Override
    public int updateWbMaintainCompany(WbMaintainCompany wbMaintainCompany)
    {
        wbMaintainCompany.setUpdateTime(DateUtils.getNowDate());
        return wbMaintainCompanyMapper.updateWbMaintainCompany(wbMaintainCompany);
    }

    /**
     * 批量删除维保公司
     * 
     * @param ids 需要删除的维保公司主键
     * @return 结果
     */
    @Override
    public int deleteWbMaintainCompanyByIds(Long[] ids)
    {
        return wbMaintainCompanyMapper.deleteWbMaintainCompanyByIds(ids);
    }

    /**
     * 删除维保公司信息
     * 
     * @param id 维保公司主键
     * @return 结果
     */
    @Override
    public int deleteWbMaintainCompanyById(Long id)
    {
        return wbMaintainCompanyMapper.deleteWbMaintainCompanyById(id);
    }

    /**
     * 根据企业认证id查询维保公司
     *
     * @param id 企业认证id
     * @return 结果
     */
    @Override
    public List<WbMaintainCompany> selectWbMaintainCompanyByAuthId(Long id) {
        return wbMaintainCompanyMapper.selectWbMaintainCompanyByAuthId(id);
    }
    /**
     * 根据用户id查询维保公司
     *
     * @param userId 用户id
     * @return 结果
     */

    @Override
    public List<WbMaintainCompany> selectWbMaintainCompanyByUserId(Long userId) {
        return wbMaintainCompanyMapper.selectWbMaintainCompanyByUserId(userId);
    }
}
